package DataStructure;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

//确定有穷自动机
public class DFA {
    private int nowStatus;
    private final Set<Integer> stopStatus;
    private final String[][] action;//状态与输入对应的动作
    private final int[][] next;//状态与输入对应的转换

    public DFA(String[][] action,int[][] next,int start,Integer[] stop) {
        this.action=action;
        this.next=next;
        this.nowStatus = start;
        this.stopStatus=new HashSet<>();
        Collections.addAll(this.stopStatus,stop);
    }

    //获取当前状态
    public int getNowStatus(){
        return nowStatus;
    }

    //执行下一步
    public void run(int input){
        if(stopStatus.contains(nowStatus)){
            System.out.println("dfa is stop!!!");
            return;
        }
        nowStatus=next[nowStatus][input];
        doAction(input);
    }

    //执行动作
    private void doAction(int input){
        String action=this.action[nowStatus][input];
        System.out.println("do action:"+action);
    }
}
